package com.cszy.yydqbfq.ui.fragment

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.MotionEvent
import android.widget.CompoundButton
import com.cszy.yydqbfq.databinding.FragmentWodeBinding
import com.cszy.yydqbfq.base.BaseVMFragment
import com.cszy.yydqbfq.common.AppConstants
import com.cszy.yydqbfq.ext.launch
import com.cszy.yydqbfq.ui.activity.ActivitySettingPwdActivity
import com.cszy.yydqbfq.ui.activity.CloseFcmActivity
import com.cszy.yydqbfq.ui.activity.FeedBackActivity
import com.cszy.yydqbfq.ui.activity.HtmlActivity
import com.cszy.yydqbfq.utils.SpUtils
import com.cszy.yydqbfq.viewmodel.MainViewModel
import com.hlyj.http.ad.NewGroMoreFeedAdManager
import com.hnyyac.ad.mds.utils.BaseAdConstants


class WodeFragment : BaseVMFragment<MainViewModel, FragmentWodeBinding>() {
    private var newGromoreFeedAd : NewGroMoreFeedAdManager? =null
    private var isUserInteraction = false
    override fun initView(savedInstanceState: Bundle?) {

    }

    override fun initData() {
        newGromoreFeedAd = NewGroMoreFeedAdManager(requireActivity(), BaseAdConstants.FEEDAD_Id, mBinding?.frameLayout,22)
        newGromoreFeedAd?.loadAd()
//        Log.e("qin",BuildConfig.CHANNEL_ID)
//        mBinding.tvChanel.text = BuildConfig.CHANNEL_ID
    }

    override fun initListener() {
       mBinding.rvXieyi.setOnClickListener {
           val intent = Intent(
               requireContext(),
               HtmlActivity::class.java
           )
           intent.putExtra("url", AppConstants.User_Agreement)
           intent.putExtra("title", "用户协议")
           startActivity(intent)
       }

      mBinding.rvYinsi.setOnClickListener {
          val intent = Intent(
              requireContext(),
              HtmlActivity::class.java
          )
          intent.putExtra("url", AppConstants.Privacy_Policy)
          intent.putExtra("title", "隐私政策")
          startActivity(intent)
      }

     mBinding.rvFeedback.setOnClickListener {
         startActivity(Intent(requireContext(), FeedBackActivity::class.java))
     }


        mBinding.switchWode.setOnTouchListener { _, event ->
            if (event.action == MotionEvent.ACTION_DOWN) {
                isUserInteraction = true
            }
            false // 必须返回false以保证Switch正常处理触摸事件
        }

    //防沉迷按钮打开关闭逻辑
        mBinding.switchWode.setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener { buttonView, isChecked ->
            if (isUserInteraction) {
               isUserInteraction = false // 重置标记
                if (isChecked){
                    launch(ActivitySettingPwdActivity::class.java)
                }else{
                    launch(CloseFcmActivity::class.java)
                }

            } else {
                Log.d("SwitchStatus", "程序化设置状态: $isChecked")
            }
        })


    }

    override fun onResume() {
        super.onResume()
        Log.e("SwitchStatus","onResume"+SpUtils.getInstance().getBoolean(AppConstants.ISOPEN_FCM))
        mBinding.switchWode.isChecked=SpUtils.getInstance().getBoolean(AppConstants.ISOPEN_FCM)
        // 创建 Intent 并设置参数
//        val serviceIntent = Intent(requireContext(), AntiAddictionService::class.java).apply {
//            // 直接调用 putExtra，不需要 requireContext()
//            if (SpUtils.getInstance().getBoolean(AppConstants.ISOPEN_FCM)) {
//                putExtra("start", true)  // 如果防沉迷开启，发送 "start"
//            } else {
//                putExtra("stop", true)   // 如果防沉迷关闭，发送 "stop"
//            }
//        }
//
//// 启动服务
//        startService(serviceIntent)
    }

}